const app = getApp();
import {
  getSmallProgramActivities
} from "../../server/api";
Page({
  data: {
    oss_url: app.globalData.oss_url,
    pageSize: 10,
    pageNum: 1,
    list: [],
    city: '',
    isFixedTop: false,
    tabIndex:0,
    tabType:'',
    have_more: true,
    submit_loading: false,
    stampTime:'',
    tabs:[
      {
        title:'全部活动',
        activeImg:'../../images/all_check.png',
        normalImg:'../../images/all.png'
      },
      // {
      //   title:'媒体发布',
      //   activeImg:'../../images/media_check.png',
      //   normalImg:'../../images/media.png'
      // },
      {
        title:'营销活动',
        activeImg:'../../images/market_check.png',
        normalImg:'../../images/market.png'
      },
      {
        title:'粉丝活动',
        activeImg:'../../images/fans_check.png',
        normalImg:'../../images/fans.png'
      },
      {
        title:'商城活动',
        activeImg:'../../images/shop_check.png',
        normalImg:'../../images/shop.png'
      },
    ]
  },
  onShow: function (options) {
    this.data.pageSize = 10
    this.data.pageNum = 1
    //  this.data.submit_loading = false
    //  this.data.have_more = true
    let timeStamp=this.currentTime();
    this.stampTime=Date.parse(timeStamp)
    this.setData({ 
      city: app.globalData.city,
      tabType:'', 
      list: [] 
    })
    this.get_list_active(this.data.tabType)
  },
  currentTime(){
    let date=new Date()
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    return [year, month, day].map(this.formatNumber).join('-')
  },
  formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  get_list_active() {
    // debugger
    // if (this.data.submit_loading || !this.data.have_more) {
    //   return
    // }

    let obj = {}
    obj['type'] = 'page'
    obj['pageSize'] = this.data.pageSize
    obj['pageNum'] = this.data.pageNum
    obj['city'] = app.globalData.city
    obj['division']=this.data.tabType
    //this.data.submit_loading = true
    getSmallProgramActivities(obj).then(res => {
     // this.data.submit_loading = false
      if (res.code == 0) {
        let arr = this.data.list
        // if (res.data.pages <= this.data.pageNum) {
        //   this.data.have_more = false
        // }
        for (let i = 0; i < res.data.list.length; i++) {
          res.data.list[i]['startTime'] = res.data.list[i]['startTime'].substr(0, 10)
          res.data.list[i]['endTime'] = res.data.list[i]['endTime'].substr(0, 10)
          res.data.list[i]['swiper'] = []
          res.data.list[i]['active_status']=""
          res.data.list[i]['startTime_stamp']=Date.parse(res.data.list[i]['startTime'])
          res.data.list[i]['endTime_stamp']=Date.parse(res.data.list[i]['endTime'])
          // console.log('开始：'+res.data.list[i]['startTime_stamp'])
          // console.log('当天:'+this.stampTime)
          // console.log('结束:'+res.data.list[i]['endTime_stamp'])
         
          if(res.data.list[i]['endTime_stamp'] > this.stampTime &&  this.stampTime >= res.data.list[i]['startTime_stamp']){
            res.data.list[i]['active_status']="进行中"
          }
          if(this.stampTime < res.data.list[i]['startTime_stamp']){
            res.data.list[i]['active_status']="即将开始"
          }
          if(this.stampTime > res.data.list[i]['ednTime_stamp']){
            res.data.list[i]['active_status']="已结束"
          }
          if (res.data.list[i]['operationImg1']) {
            res.data.list[i]['swiper'].push(res.data.list[i]['operationImg1'])
          }
          if (res.data.list[i]['operationImg2']) {
            res.data.list[i]['swiper'].push(res.data.list[i]['operationImg2'])
          }
          if (res.data.list[i]['operationImg3']) {
            res.data.list[i]['swiper'].push(res.data.list[i]['operationImg3'])
          }
        }
        arr.push(...res.data.list)
        this.setData({ list: arr })
      }
    })
  },
  to_show(e) {
    wx.navigateTo({
      url: '/pages/gift/show/index?id=' + e.currentTarget.dataset['id']
    })
  },
  selectTab(e){
    this.data.list=[]
    let activeType=e.currentTarget.dataset.type;
    if(activeType=="全部活动"){
      activeType=''
    }
    this.setData({
      tabIndex:e.currentTarget.dataset.index,
      tabType:activeType
    })
    this.get_list_active()
  },
  to_location() {
    wx.navigateTo({
      url: '/pages/main/chooseACity/index',
    })
  },
  onPageScroll: function (e) {
    if (e.scrollTop >= 300) {
      this.setData({
        isOperate: true,
      })
    } else {
      this.setData({
        isOperate: false
      })
    }
  },
  onReachBottom() {
    this.data.pageNum++
   // this.get_list_active()
  }
})